﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using HuaweiCloud.Dotnet.Samples.ESports.Dbs;
using HuaweiCloud.Dotnet.Samples.ESports.Entities;
using HuaweiCloud.Dotnet.Samples.ESports.Services;

namespace HuaweiCloud.Dotnet.Samples.ESports.Pages.Steps
{
    public class EditModel : PageModel
    {
        private readonly IStepAppService _service;

        public EditModel(IStepAppService service)
        {
            _service = service;
        }

        [BindProperty]
        public Step Step { get; set; } = default!;

        public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var step =  await _service.GetAsync(id.Value);
            if (step == null)
            {
                return NotFound();
            }
            Step = step;
            return Page();
        }

        // To protect from overposting attacks, enable the specific properties you want to bind to.
        // For more details, see https://aka.ms/RazorPagesCRUD.
        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            await _service.UpdateAsync(Step);
            
            return RedirectToPage("./Index");
        }

        //private async Task<bool> StepExists(int id)
        //{
        //    return (await _service.GetAsync(id)) != null;
        //}
    }
}
